package com.biz.primus.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * 价格工具
 *
 * @author david-liu
 * @date 2016年12月25日
 * @reviewer
 * @see
 */
public class PriceUtil implements Serializable {

    private static final long serialVersionUID = -7842608514145095611L;

    private static final BigDecimal CENT_AND_YUAN_DIVIDER = new BigDecimal(100);

    public static Integer getCent2yuanPrice(Integer cents) {
        // 如果为空, 返回0
        if (cents == null || cents == 0) {
            return ValueUtils.getValue(cents);
        }

        return new BigDecimal(cents).divide(CENT_AND_YUAN_DIVIDER, BigDecimal.ROUND_UP).intValue();

    }

    public static String getCent2yuanPriceString(Integer cents) {
        return String.valueOf(getCent2yuanPrice(cents));
    }
    
    /**
	 * 价格 分 大于0时  向上进位
	 * @author huiwu.heng
	 * @date 2017年4月25日
	 * @param finalPrice
	 * @return
	 */
	public static Integer priceOfCentUp(Integer finalPrice){
        if(finalPrice != null){
        	String price = String.valueOf(finalPrice);
        	Integer num_1 = 0;
            if(StringUtils.isNotBlank(price.substring(0, price.length()-1))){
            	num_1 = Integer.valueOf(price.substring(0, price.length()-1));
            }
            Integer num_2 = 0;
            if(StringUtils.isNotBlank(price.substring(price.length()-1))){
            	num_2 = Integer.valueOf(price.substring(price.length()-1));
            }
            if(price.length() == 1){
            	if(num_2 > 0){
                	num_2 = 0;
                	num_1 = 1;
                }
            }else {
            	if(num_2 > 0){
            		num_2 = 0;
            		num_1++;
            	}
            }
            
            
            StringBuilder strPrice = new StringBuilder();
            strPrice.append(String.valueOf(num_1)).append(Integer.valueOf(num_2));
            finalPrice = Integer.valueOf(strPrice.toString());
    		return finalPrice;
        }else {
        	return 0;
        }
	}
}
